package com.uchedao.seller.widget.loadstate.listview.swiperefresh;


import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;

import com.uchedao.seller.R;
import com.uchedao.seller.inf.ILoadResult;
import com.uchedao.seller.widget.loadstate.listview.RefreshListView;
import com.uchedao.seller.widget.loadstate.listview.RefreshListView.IListViewCallBack;
import com.uchedao.seller.widget.loadstate.listview.footview.FootView;


/**
 * 默认下拉刷新
 *
 * @author wushan
 */
public class ReDefaultSwipeRefreshLayout extends SwipeRefreshLayout implements ILoadResult {

    protected RefreshListView mListView;
    private BaseAdapter mAdapter;

    public ReDefaultSwipeRefreshLayout(Context context, AttributeSet attrs) {
        super(context, attrs);

    }

    /**
     * 初始化
     */
    public void init() {
        setColorScheme(R.color.swipe_refresh_color);
        mListView = (RefreshListView) findViewById(R.id.comm_refresh_lv);
        FootView footView = new FootView(getContext());
        mListView.addBaseFootView(footView);
    }

    public RefreshListView getmListView() {
        return mListView;
    }


    /**
     * 设置头部
     *
     * @param headView
     */
    public void addHeaderView(View headView) {
        mListView.addHeaderView(headView);

    }

    /**
     * 设置适配器
     *
     * @param adapter
     */
    public void setAdapter(BaseAdapter adapter) {
        mAdapter = adapter;
        mListView.setAdapter(mAdapter);
    }

    /**
     * 上下拉刷新
     *
     * @param callBack
     * @param onRefreshListener
     */
    public void setUpAndDownRefresh(IListViewCallBack callBack, OnRefreshListener onRefreshListener) {
        if (callBack != null) {
            mListView.setIListViewCallBack(callBack);
        }
        if (onRefreshListener != null) {
            setOnRefreshListener(onRefreshListener);
        }
    }

    public void smoothScrollToIndex(int i) {
        mListView.smoothScrollToPosition(i);

    }

    public void setOnItemClickListener(OnItemClickListener clickListener) {
        if (clickListener != null)
            mListView.setOnItemClickListener(clickListener);
    }

    @Override
    public void onLoadFailed() {
        setRefreshing(false);
        mListView.onLoadFailed();
    }

    @Override
    public void onLoadSuccess() {
        setRefreshing(false);
        mListView.onLoadSuccess();
    }

    @Override
    public void onLoadComplete() {
        setRefreshing(false);
        mListView.onLoadComplete();
    }

    @Override
    public void onLoadEmpty() {
        setRefreshing(false);
        mListView.onLoadEmpty();
    }

    @Override
    public void onLoadStart() {
        setRefreshing(false);
        mListView.onLoadStart();

    }

}
